package cn.factorybox.item.controller;

import cn.factorybox.common.entity.Result;
import cn.factorybox.item.annotation.ControllerLogAnnotation;
import cn.factorybox.item.service.CameraService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-05-25 9:08
 */

@Slf4j
@Api(tags = "摄像头管理模块")
@RestController
@RequestMapping("camera")
public class CameraController {

    @Autowired
    private CameraService cameraService;

    @ControllerLogAnnotation(moduleName = "摄像头管理模块", functionName = "添加新的摄像头")
    @ApiOperation(value = "添加摄像头", notes = "添加摄像头")
    @PostMapping("add")
    public Result addDevice(@RequestBody Map<String, Object> map) {
        cameraService.addDevice(map);
        return Result.ok("添加摄像头成功");
    }

    @ControllerLogAnnotation(moduleName = "摄像头管理模块", functionName = "设置摄像头")
    @ApiOperation(value = "设置摄像头", notes = "设置摄像头")
    @PutMapping("edit")
    public Result updateDevice(@RequestBody Map<String, Object> map) {
        cameraService.updateDevice(map);
        return Result.ok("设置摄像头成功");
    }

    @ControllerLogAnnotation(moduleName = "摄像头管理模块", functionName = "获取摄像头设备列表")
    @ApiOperation(value = "获取设备列表", notes = "获取设备列表")
    @GetMapping("list")
    public Result selectList() {
        Map<String, Object> map = cameraService.selectList();
        return Result.ok(map, "成功获取设备数据");
    }

    @ControllerLogAnnotation(moduleName = "摄像头管理模块", functionName = "保存照片")
    @ApiOperation(value = "保存照片", notes = "保存照片")
    @PostMapping("savePic")
    public Result savePicByFormData(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        String deviceId = request.getHeader("deviceID");
        log.info("deviceID设备号：{}",deviceId);
        boolean result = cameraService.savePic(file, deviceId);
        Map<String, Object> map = new HashMap<>();
        if (result) {
            map.put("getmessage", "success");
            return Result.ok(map);
        } else {
            map.put("getmessage", "fail");
            return Result.ok(map);
        }
    }

    @ControllerLogAnnotation(moduleName = "摄像头管理模块", functionName = "获取今日图像统计信息")
    @ApiOperation(value = "获取今日统计信息", notes = "获取今日统计信息")
    @GetMapping("count")
    public Result getTodayCount(@RequestParam("deviceID") String deviceID) {
        Map<String, Object> map = cameraService.getTodayCount(deviceID);
        return Result.ok(map, "获取数据成功");
    }

    @ControllerLogAnnotation(moduleName = "摄像头管理模块", functionName = "获取图像列表")
    @ApiOperation(value = "获取图像列表", notes = "获取图像列表")
    @GetMapping("piclist")
    public Result getPicList(@RequestParam Map<String, Object> map) throws ParseException {
        Map<String, Object> list = cameraService.selectPicList(map);
        return Result.ok(list, "成功获取图像列表数据");
    }

    @ControllerLogAnnotation(moduleName = "摄像头管理模块", functionName = "获取全天图像信息列表")
    @ApiOperation(value = "获取全天图像信息列表", notes = "获取全天图像信息列表")
    @GetMapping("alldayPic")
    public Result getAllDayMessage(@RequestParam Map<String, Object> map) throws ParseException {
        Map<String, Object> list = cameraService.selectAllDayList(map);
        return Result.ok(list, "成功获取图像设备列表数据");
    }

}
